package com.dxj1992.controller.admin.system;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dxj1992.entity.AdminPermissions;
import com.dxj1992.service.IAdminPermissionsService;
import com.dxj1992.utils.ClearCache;
import com.dxj1992.utils.CommonResult;
import com.dxj1992.entity.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-31
 */
@Controller
@RequestMapping("/admin/system/permission")
public class AdminPermissionsController {

    @Autowired
    private IAdminPermissionsService adminPermissionsService;
    @Autowired
    private ClearCache clearCache;

    @GetMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "admin/system/permission/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public List<AdminPermissions> list(AdminPermissions adminPermissions) {
        LambdaQueryWrapper<AdminPermissions> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(adminPermissions.getMenuName())) {
            queryWrapper.like(AdminPermissions::getMenuName, adminPermissions.getMenuName());
        }

        if (Objects.nonNull(adminPermissions.getStatus())) {
            queryWrapper.eq(AdminPermissions::getStatus, adminPermissions.getStatus());
        }

        List<AdminPermissions> list = adminPermissionsService.list(queryWrapper);
        return list;
    }

    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap) {
        AdminPermissions menu = null;
        if (parentId > 0) {
            menu = adminPermissionsService.getOne(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getId, parentId));
            mmap.put("menu", menu);
        } else {
            menu = new AdminPermissions();
            menu.setId(0);
            menu.setMenuName("主目录");
            mmap.put("menu", menu);
        }
        return "admin/system/permission/add";
    }

    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public Integer checkMenuNameUnique(@Param("menuName") String menuName, @Param("parentId") Integer parentId, @Param("id") Integer id) {

        AdminPermissions menu = adminPermissionsService.getOne(Wrappers.<AdminPermissions>lambdaQuery().ne(AdminPermissions::getId, id).eq(AdminPermissions::getParentId, parentId).eq(AdminPermissions::getMenuName, menuName));
        if (Objects.isNull(menu)) {
            return 0;
        }
        return 1;
    }

    @PostMapping("/add")
    @ResponseBody
    public CommonResult addSave(AdminPermissions adminPermissions) {
        boolean save = adminPermissionsService.save(adminPermissions);
        clearCache.clearAdminCache();
        if (!save) {
            CommonResult.failed("添加失败");
        }
        return CommonResult.success("添加成功");
    }

    @GetMapping("/selectMenuTree/{id}")
    public String selectMenuTree(@PathVariable("id") String id, ModelMap mmap) {
        AdminPermissions adminPermissions = adminPermissionsService.getOne(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getId, id));
        mmap.put("menu", adminPermissions);
        return "admin/system/permission/tree";
    }

    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData() {
        List<Ztree> adminPermissions = adminPermissionsService.menuTreeData(null);
        return adminPermissions;
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon")
    public String icon() {
        return "admin/system/permission/icon";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        AdminPermissions menu = adminPermissionsService.getOne(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getId, id));
        AdminPermissions parentMenu = adminPermissionsService.getOne(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getId, menu.getParentId()));
        if (Objects.nonNull(parentMenu)) {
            menu.setParentName(parentMenu.getMenuName());
        }
        mmap.put("menu", menu);
        return "admin/system/permission/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public CommonResult editSave(AdminPermissions adminPermissions) {
        boolean b = adminPermissionsService.updateById(adminPermissions);
        clearCache.clearAdminCache();
        if (!b) {
            CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @GetMapping("/remove/{id}")
    @ResponseBody
    public CommonResult edit(@PathVariable("id") Integer id) {

        int count = adminPermissionsService.count(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getParentId, id));
        if (count > 0){
            return CommonResult.failed("请删除子菜单后，再尝试~");
        }

        boolean remove = adminPermissionsService.remove(Wrappers.<AdminPermissions>lambdaQuery().eq(AdminPermissions::getId, id));
        clearCache.clearAdminCache();
        if (!remove) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(@Param("roleId") Integer roleId) {
        List<Ztree> adminPermissions = adminPermissionsService.menuTreeData(roleId);
        return adminPermissions;
    }
}
